package org.hcyspartnoc.demo.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import org.hcyspartnoc.demo.common.core.AjaxResult;
import org.hcyspartnoc.demo.common.core.PageBean;
import org.hcyspartnoc.demo.common.core.annotation.Log;
import org.hcyspartnoc.demo.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.demo.common.core.enums.BusinessType;
import org.hcyspartnoc.demo.common.exception.system.SysRoleException;
import org.hcyspartnoc.demo.pojo.dto.system.*;
import org.hcyspartnoc.demo.pojo.po.system.SysRolePo;
import org.hcyspartnoc.demo.pojo.vo.system.SysRolePageDataVo;
import org.hcyspartnoc.demo.pojo.vo.system.SysRoleVo;
import org.hcyspartnoc.demo.pojo.vo.system.SysRolequeryAllVo;
import org.hcyspartnoc.demo.pojo.vo.system.SysUserVo;
import org.hcyspartnoc.demo.service.system.ISysRoleService;
import org.hcyspartnoc.demo.service.system.ISysUserService;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping
public class SysRoleController {
    @Resource
    private ISysRoleService sysRoleService;

    @Log(title = "系统·角色·新增", businessType = BusinessType.INSERT)
    @PreAuthorize(auth = "system:role:add", descr = "新增系统角色")
    @PostMapping("/admin/user/role/add")
    public AjaxResult add(@Valid @RequestBody SysRoleAddDto sysRoleAddDto) throws SysRoleException {
        sysRoleService.addRolebyper(sysRoleAddDto);
        return AjaxResult.success();
    }

    @Log(title = "系统·角色·修改", businessType = BusinessType.UPDATE)
    @PreAuthorize(auth = "system:role:modify", descr = "修改系统角色")
    @PutMapping("/modify")
    public AjaxResult modify(@Valid @RequestBody SysRoleModifyDto sysRoleModifyDto) throws SysRoleException {
        SysRoleVo sysRoleVo = sysRoleService.modifyRole(sysRoleModifyDto);
        return AjaxResult.success(sysRoleVo);
    }

    @Log(title = "系统·角色·删除·通过id", businessType = BusinessType.DELETE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:remove:id", descr = "删除系统角色")
    @DeleteMapping("/admin/user/role/delete")
    public AjaxResult remove( @RequestBody SysRoledeleteDto sysRoledeleteDto) throws SysRoleException {
        String[] roleIds = sysRoledeleteDto.getIds().split(",");
        for (String roleid :roleIds)
        {
            Integer role= Integer.parseInt(roleid);
            sysRoleService.removerolebyid(role);
        }
        return AjaxResult.successMsg();
    }
    @Log(title = "系统·角色·查询·分页", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:query:page", descr = "分页查询系统角色")
    @PostMapping("/admin/user/role/list")
    public AjaxResult queryByPage(@RequestBody SysRoleQueryPageDto sysRoleQueryPageDto) {

        PageBean<SysRolePo, SysRoleVo> pageBean = sysRoleService.queryByPage(sysRoleQueryPageDto);
        ArrayList<SysRoleVo> records = pageBean.getList();
        long total = pageBean.getTotal();
        SysRolePageDataVo data=new SysRolePageDataVo(records,total);
        return AjaxResult.success(data);
    }

    @Log(title = "系统·角色·查询·所有", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:query:all", descr = "查询所有角色")
    @GetMapping("/admin/user/user/getRoles")
    public AjaxResult queryAll() {
        List<SysRolePo> list = sysRoleService.list();
        ArrayList<SysRolequeryAllVo> sysRoleVos = new ArrayList<>();
        for (SysRolePo sysRolePo : list) {
            sysRoleVos.add(BeanUtil.copyProperties(sysRolePo, SysRolequeryAllVo.class));
        }
        return AjaxResult.success(sysRoleVos);
    }

    @Log(title = "系统·角色·修改·状态", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:role:modify:status", descr = "修改角色状态")
    @PutMapping("/modify/status/{roleId}")
    public AjaxResult modifyStatus(@PathVariable Integer roleId) throws SysRoleException {
        sysRoleService.modifyStatus(roleId);
        return AjaxResult.successMsg();
    }
    @Log(title = "系统·角色·检查·存在", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:roleName:check:exist", descr = "角色名检查是否存在")
    @PostMapping("/admin/user/role/checkRoleName")
    public AjaxResult checkRoleName(@RequestBody SysRoleNameCheckDto sysRoleNameCheckdto){
        String name=sysRoleNameCheckdto.getName();
        QueryWrapper<SysRolePo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        boolean exists = sysRoleService.exists(queryWrapper);
        return  AjaxResult.success(exists);
    }

    @Log(title = "系统·角色·修改", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:roleName:update", descr = "角色修改")
    @PostMapping("/admin/user/role/save")
    public AjaxResult roleNameupdate(@RequestBody SysRoleUpdateDto sysRoleUpdate){
        if(sysRoleUpdate==null){
            throw  new RuntimeException("信息不准为空");
        }
        sysRoleService.sysRoleUpdate(sysRoleUpdate);
        return AjaxResult.success();
    }

}
